package com.ruiysoft.service.feight;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.entity.feight.Freight;
import com.ruiysoft.entity.feight.FreightRegion;
import com.ruiysoft.mapper.feight.FreightMapper;
import com.ruiysoft.mapper.feight.FreightRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyahui
 * @since 2018-11-10
 */
@Service
@Transactional
public class FreightServiceImpl extends ServiceImpl<FreightMapper, Freight> implements IFreightService {

    @Autowired
    private FreightMapper freightMapper;
    @Autowired
    private FreightRegionMapper freightRegionMapper;

    @Override
    public Boolean saveFreight(@Valid Freight freight) throws Exception {
        freightMapper.insert(freight);
        List<FreightRegion> freightRegionList = freight.getFreightRegionList();
        if(null != freightRegionList && !freightRegionList.isEmpty()){
            for(FreightRegion freightRegion:freightRegionList){
                freightRegion.setFreightId(freight.getId());
                freightRegionMapper.insert(freightRegion);
            }
        }
        return true;
    }

    @Override
    public Boolean updateFreight(@Valid Freight freight) throws Exception {
        freightMapper.updateById(freight);
        freightRegionMapper.delete(new UpdateWrapper<FreightRegion>().lambda().eq(FreightRegion::getFreightId,freight.getId()));
        List<FreightRegion> freightRegionList = freight.getFreightRegionList();
        if(null != freightRegionList && !freightRegionList.isEmpty()){
            for(FreightRegion freightRegion:freightRegionList){
                freightRegion.setFreightId(freight.getId());
                freightRegionMapper.insert(freightRegion);
            }
        }
        return true;
    }

    @Override
    public List<Freight> freightList(Long shopId,Long freightId, String freightName, String adcode) {
        return freightMapper.freightList(shopId,freightId,freightName,adcode);
    }
}
